{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0d303ee7",
   "metadata": {},
   "source": [
    "# 2023-11-6\n",
    "# 课程：Python语言\n",
    "# week09课堂"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "92ab0c01",
   "metadata": {},
   "source": [
    "# 购物车程序项目：\n",
    "> 1. 运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户\n",
    "  >> A. 列表书籍（第一种：自己创建的；第二种：空列表，后面补充数据）\n",
    "> 2. 让用户输入商品编号进行商品的购买\n",
    "> 3. 用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户\n",
    "> 4. 用户可以随时退出购买，退出时打印用户已购买的商品和支付宝余额"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "01edf127",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入余额:1111\n",
      "-----小王商铺-----\n",
      "0,商品名称ihpone15  ,价格为:9999元\n",
      "1,商品名称mate60 Pro  ,价格为:6999元\n",
      "2,商品名称草莓奶昔 （蜜雪冰城）,价格为:7元\n",
      "3,商品名称个人自传 课本（美女著） ,价格为:88元\n"
     ]
    },
    {
     "ename": "IndexError",
     "evalue": "list index out of range",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[6], line 14\u001b[0m\n\u001b[0;32m     12\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m-----小王商铺-----\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m     13\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m i \u001b[38;5;129;01min\u001b[39;00m 商品列表:\n\u001b[1;32m---> 14\u001b[0m      \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m,商品名称\u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m,价格为:\u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m元\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;241m%\u001b[39m(商品列表\u001b[38;5;241m.\u001b[39mindex(i),i[\u001b[38;5;241m0\u001b[39m],i[\u001b[38;5;241m1\u001b[39m]))\n",
      "\u001b[1;31mIndexError\u001b[0m: list index out of range"
     ]
    }
   ],
   "source": [
    "alipay = input(\"请输入余额:\") # 用户输入\n",
    "商品列表 = [\n",
    "    [\"ihpone15  \",9999],\n",
    "    [\"mate60 Pro  \",6999],\n",
    "    [\"草莓奶昔 （蜜雪冰城）\",7],\n",
    "        [\"个人自传 课本（美女著） \",88],\n",
    "    [\"电瓶车（二手） 800\"]\n",
    "\n",
    "]\n",
    "\n",
    "# print(商品列表)\n",
    "print(\"-----小王商铺-----\")\n",
    "for i in 商品列表:\n",
    "     print(\"%s,商品名称%s,价格为:%s元\"%(商品列表.index(i),i[0],i[1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f0064537",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "输入余额：111\n",
      "-------小王商铺————\n",
      "0,商品名称ihphone,价格为:8999元\n",
      "1,商品名称mate60 pro,价格为:6999元\n",
      "2,商品名称草莓奶盖（蜜雪冰城）,价格为:7元\n"
     ]
    }
   ],
   "source": [
    "alipay = input(\"输入余额：\")\n",
    "\n",
    "商品列表 = [\n",
    "    [\"ihphone\" ,8999],\n",
    "    [\"mate60 pro\",6999],\n",
    "    [\"草莓奶盖（蜜雪冰城）\",7]\n",
    "]\n",
    "print(\"-------小王商铺————\")\n",
    "for i in 商品列表:\n",
    "    print(\"%s,商品名称%s,价格为:%s元\"%(商品列表.index(i),i[0],i[1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "ab305606",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "用户选择的商品编号：989786\n",
      "111 <class 'str'>\n",
      "989786 <class 'str'>\n"
     ]
    },
    {
     "ename": "IndexError",
     "evalue": "list index out of range",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[10], line 7\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[38;5;28mprint\u001b[39m(用户选择,\u001b[38;5;28mtype\u001b[39m(用户选择))\n\u001b[0;32m      6\u001b[0m \u001b[38;5;66;03m# print(商品列表[int(用户选择)][1],type(商品列表[用户选择][1]))\u001b[39;00m\n\u001b[1;32m----> 7\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m alipay \u001b[38;5;241m>\u001b[39m 商品列表[\u001b[38;5;28mint\u001b[39m(用户选择)][\u001b[38;5;241m1\u001b[39m]:\n\u001b[0;32m      8\u001b[0m     \u001b[38;5;66;03m# 1. 钱够！扣钱，加入购物车\u001b[39;00m\n\u001b[0;32m      9\u001b[0m     alipay \u001b[38;5;241m=\u001b[39m alipay \u001b[38;5;241m-\u001b[39m 商品列表[用户选择][\u001b[38;5;241m1\u001b[39m]\n\u001b[0;32m     10\u001b[0m     shopping_car\u001b[38;5;241m.\u001b[39mappend(商品列表[用户选择])\n",
      "\u001b[1;31mIndexError\u001b[0m: list index out of range"
     ]
    }
   ],
   "source": [
    "# 2\n",
    "用户选择 = input(\"用户选择的商品编号：\")\n",
    "# 增加进购物车，扣钱？\n",
    "print(alipay,type(alipay))\n",
    "print(用户选择,type(用户选择))\n",
    "# print(商品列表[int(用户选择)][1],type(商品列表[用户选择][1]))\n",
    "if alipay > 商品列表[int(用户选择)][1]:\n",
    "    # 1. 钱够！扣钱，加入购物车\n",
    "    alipay = alipay - 商品列表[用户选择][1]\n",
    "    shopping_car.append(商品列表[用户选择])\n",
    "    \n",
    "print(shopping_car)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bfd90723",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cd45df47",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
